<!DOCTYPE html>
<html class="x-admin-sm">
  
  <head>
    <meta charset="UTF-8">
    <title>权限编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/index.css">
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
   <script type="text/javascript" src="./js/index.js"></script>
   
  </head>
  
  <body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form  id="authform" method="post" class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>权限名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="auth_name" name="auth_name" required="" lay-verify="required"
                        autocomplete="off" class="layui-input">
                    </div>
                    
                </div>
                
                     <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                       <span class="x-red">*</span>绑定菜单
                    </label>
                    <div class="layui-input-inline">
                        <select  id="memuid"  name="memuid" 
                        autocomplete="off" class="layui-input">
                
                         
                        </select>
                    </div>
                </div>
                               
                <input type="text" style="display: none;" id="id" name="id" />
              
                <div class="layui-form-item layui-form-text">
                    <label for="desc" class="layui-form-label">
                        描述
                    </label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="auth_desc" name="auth_desc" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                
                <input type="button" value="保存" class="layui-btn"  onclick="add()">
              </div>
            </form>
        </div>
    </div>
    <script src="js/jquery.js"></script>
    <script>
    	 	//查询所有的权限信息
    	 	selects();
    	function selects(){
    	
    		$.ajax({
    			type:"get",
    			url:"http://localhost:16111/memurbac/selectidauth",
    			xhrFields:{withCredentials: true},
    			dataType:"json",    			
    			data:{auth_id:getUrlVar("authid")},
    			success:function(res){
    				//表示为查询成功，
    				if(res.code==200){
    			
    			
    			
    					   	//添加菜单列表
    		          //	$('#memuid').html("");
    					for(var i in res.data.memus){
    						
    						var str="<option id=\"memuid"+res.data.memus[i].id+"\" value=\""+res.data.memus[i].id+"\">"+res.data.memus[i].memu_name+"</option>"
    					$('#memuid').html($('#memuid').html()+str);
    					}
    					
          
    					//将权限数据充填到页面中
    				  
    					$('#auth_name').val(res.data.auth.auth_name);
    					$('#memuid'+res.data.auth.memuid).attr("selected",true);
    			$('#auth_desc').val(res.data.auth.auth_desc);
    			$('#id').val(res.data.auth.id);
    			
    			
    					 //重新加載js文件
    					   loadJs("./lib/layui/lay/modules/form.js");
    			
    					  
    				
    				}else{
    					
    					layui.use("layer", function () {
                //提示错误信息
    					layer.msg(res.error,{icon:7,time:1000});
             })

    					
    				
    				}
    			},
    			error:function(error){
    			
    				layui.use("layer", function (){
             	//提示错误信息
    				layer.msg(JSON.stringify(error),{icon:2,time:1000});
             })
    				
    			}
    				
    		});
   
    	}
         //监听提交
           function add(){ 
          	
     
       
         
         
            //发异步，把数据提交给服务器
             		$.ajax({
    			type:"get",
    			url:"http://localhost:16111/memurbac/updauth",
    			xhrFields:{withCredentials: true},
    			dataType:"json",    			
    			data:$('#authform').serialize(),
    			success:function(res){
    				//表示为修改权限成功
    			if(res.code==200){
    			    	//提示成功信息
    						layui.use("layer", function () {
              
            layer.alert(res.data, {icon: 6},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                 parent.selects();
                //关闭当前frame
                parent.layer.close(index);
            });
          });
    					 
            
    
    				}else{
    					//提示错误信息
    						layui.use("layer", function () {
               layer.alert(res.error, {icon: 7},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            });
            
       });
    						
    					
    				}
    			},
    			error:function(error){
    				//提示错误信息
    						layui.use("layer", function () {
              		 layer.alert(error, {icon: 2},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            });
            
       });
    						
    			}
    				
    		});
          }

        
            //重新加载js文件
        function loadJs(file)
{
    var head = $("head").remove("script[role='reload']");
    $("<scri"+"pt>"+"</scr"+"ipt>").attr({ 
    role:'reload',src:file,type:'text/javascript'}).appendTo(head);
} 
           function getUrlVars(){    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

//得到指定参数的value
function getUrlVar(name){
    return getUrlVars()[name];
}
    </script>
  
  </body>

</html>